


<!DOCTYPE html>
<html id="htmlId">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 
  <title>Coverage Report > RedisUtil</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/idea.min.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.min.js"></script>
  <script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">com.ie.common.security.utils</a>
</div>

<h1>Coverage Summary for Class: RedisUtil (com.ie.common.security.utils)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Branch, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">RedisUtil</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    5.1%
  </span>
  <span class="absValue">
    (2/39)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/32)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    1.5%
  </span>
  <span class="absValue">
    (2/132)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package com.ie.common.security.utils;
&nbsp;
&nbsp;import cn.hutool.json.JSONUtil;
&nbsp;import lombok.extern.slf4j.Slf4j;
&nbsp;import org.springframework.beans.factory.annotation.Autowired;
&nbsp;import org.springframework.data.redis.core.RedisTemplate;
&nbsp;import org.springframework.stereotype.Component;
&nbsp;import org.springframework.util.CollectionUtils;
&nbsp;
&nbsp;import java.util.Collection;
&nbsp;import java.util.List;
&nbsp;import java.util.Map;
&nbsp;import java.util.Set;
&nbsp;import java.util.concurrent.TimeUnit;
&nbsp;
&nbsp;/**
&nbsp; * @Description: Redis工具类
&nbsp; * @Author: xujia
&nbsp; * @Date: 2024/8/29  23:03
&nbsp; * @Param:
&nbsp; * @version: 1.0
&nbsp; * @Return:
&nbsp; **/
&nbsp;@Component
<b class="fc">&nbsp;@Slf4j</b>
<b class="fc">&nbsp;public final class RedisUtil {</b>
&nbsp;    @Autowired
&nbsp;    private RedisTemplate&lt;String, Object&gt; redisTemplate;
&nbsp;    // =============================common============================
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 指定缓存失效时间
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  13:54
&nbsp;     * @Param: [key, time]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean
&nbsp;     **/
&nbsp;    public void expire(String key, long time) {
&nbsp;        try {
<b class="nc">&nbsp;            if (time &gt; 0) {</b>
<b class="nc">&nbsp;                redisTemplate.expire(key, time, TimeUnit.SECONDS);</b>
&nbsp;            }
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 判断key是否存在
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  13:56
&nbsp;     * @Param: [key]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean, true 存在 false不存在
&nbsp;     **/
&nbsp;    public boolean hasKey(String key) {
&nbsp;        try {
<b class="nc">&nbsp;            return redisTemplate.hasKey(key);</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 删除缓存
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  13:57
&nbsp;     * @Param: [key], 可以传一个值 或多个
&nbsp;     * @version: 1.0
&nbsp;     * @Return: void
&nbsp;     **/
&nbsp;    @SuppressWarnings(&quot;unchecked&quot;)
&nbsp;    public void del(String... key) {
<b class="nc">&nbsp;        if (key != null &amp;&amp; key.length &gt; 0) {</b>
<b class="nc">&nbsp;            if (key.length == 1) {</b>
<b class="nc">&nbsp;                redisTemplate.delete(key[0]);</b>
&nbsp;            } else {
<b class="nc">&nbsp;                redisTemplate.delete((Collection&lt;String&gt;) CollectionUtils.arrayToList(key));</b>
&nbsp;            }
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 根据key 获取过期时间
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  13:55
&nbsp;     * @Param: [key]不能为null
&nbsp;     * @version: 1.0
&nbsp;     * @Return: long 时间(秒) 返回0代表为永久有效
&nbsp;     **/
&nbsp;    public long getExpire(String key) {
<b class="nc">&nbsp;        return redisTemplate.getExpire(key, TimeUnit.SECONDS);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 通过给定的前缀perFix从Redis中查找所有匹配的键。将找到的键集转换为字符串数组。
&nbsp;     * 调用del方法删除这些键,返回true表示操作成功。
&nbsp;     * @Author: xujia
&nbsp;     * @Date: 2024/8/29  22:47
&nbsp;     * @Param: [perFix]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean
&nbsp;     **/
&nbsp;    public boolean deleteKeysByPerFix(String perFix) {
<b class="nc">&nbsp;        Set&lt;String&gt; keys = this.redisTemplate.keys(perFix);</b>
<b class="nc">&nbsp;        String[] mykeys = new String[keys.size()];</b>
<b class="nc">&nbsp;        keys.toArray(mykeys);</b>
<b class="nc">&nbsp;        this.del(mykeys);</b>
<b class="nc">&nbsp;        return true;</b>
&nbsp;    }
&nbsp;    // ============================String=============================
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 普通缓存获取
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  13:58
&nbsp;     * @Param: [key]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: java.lang.Object
&nbsp;     **/
&nbsp;    public Object get(String key) {
<b class="nc">&nbsp;        return key == null ? null : redisTemplate.opsForValue().get(key);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 普通缓存放入
&nbsp;     * https://blog.csdn.net/qq_43842093/article/details/121527498
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  13:58
&nbsp;     * @Param: [key, value]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean, true成功 false失败
&nbsp;     **/
&nbsp;    public boolean set(String key, Object value) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForValue().set(key, value);</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 普通缓存放入并设置时间
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  13:59
&nbsp;     * @Param: [key, value, time] 键值，时间(秒) time要大于0 如果time小于等于0 将设置无限期
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean
&nbsp;     **/
&nbsp;    public boolean set(String key, Object value, long time) {
&nbsp;        try {
<b class="nc">&nbsp;            if (time &gt; 0) {</b>
<b class="nc">&nbsp;                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);</b>
&nbsp;            } else {
<b class="nc">&nbsp;                set(key, value);</b>
&nbsp;            }
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 递增
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:00
&nbsp;     * @Param: [key, delta] 键,要增加几(大于0)
&nbsp;     * @version: 1.0
&nbsp;     * @Return: long
&nbsp;     **/
&nbsp;    public long incr(String key, long delta) {
<b class="nc">&nbsp;        if (delta &lt; 0) {</b>
<b class="nc">&nbsp;            throw new RuntimeException(&quot;递增因子必须大于0&quot;);</b>
&nbsp;        }
<b class="nc">&nbsp;        Long result = redisTemplate.opsForValue().increment(key, delta);</b>
<b class="nc">&nbsp;        if (result == null) {</b>
&nbsp;            // Handle null case (throw exception, return default value, etc.)
<b class="nc">&nbsp;            throw new IllegalStateException(&quot;Redis increment returned null&quot;);</b>
&nbsp;        }
&nbsp;        // long primitiveResult = result;
<b class="nc">&nbsp;        return result;</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 递减
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:02
&nbsp;     * @Param: [key, delta] 键,要减少几(小于0)
&nbsp;     * @version: 1.0
&nbsp;     * @Return: long
&nbsp;     **/
&nbsp;    public long decr(String key, long delta) {
<b class="nc">&nbsp;        if (delta &lt; 0) {</b>
<b class="nc">&nbsp;            throw new RuntimeException(&quot;递减因子必须大于0&quot;);</b>
&nbsp;        }
<b class="nc">&nbsp;        Long result = redisTemplate.opsForValue().increment(key, -delta);</b>
<b class="nc">&nbsp;        if (result == null) {</b>
&nbsp;            // Handle null case (throw exception, return default value, etc.)
<b class="nc">&nbsp;            throw new IllegalStateException(&quot;Redis increment returned null&quot;);</b>
&nbsp;        }
&nbsp;        // long primitiveResult = result;
<b class="nc">&nbsp;        return result;</b>
&nbsp;    }
&nbsp;
&nbsp;    // ================================Map=================================
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: HashGet
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:11
&nbsp;     * @Param: [key, item] 不能为null
&nbsp;     * @version: 1.0
&nbsp;     * @Return: java.lang.Object
&nbsp;     **/
&nbsp;    public Object hget(String key, String item) {
<b class="nc">&nbsp;        return redisTemplate.opsForHash().get(key, item);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 取hashKey对应的所有键值
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:12
&nbsp;     * @Param: [key]
&nbsp;     * @version: 1.0
&nbsp;     * @Return: java.util.Map&lt;java.lang.Object, java.lang.Object&gt;对应的多个键值
&nbsp;     **/
&nbsp;    public Map&lt;Object, Object&gt; hmget(String key) {
<b class="nc">&nbsp;        return redisTemplate.opsForHash().entries(key);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: HashSet
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:13
&nbsp;     * @Param: [key, map]对应多个键值
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean true 成功 false 失败
&nbsp;     **/
&nbsp;    public boolean hmset(String key, Map&lt;String, Object&gt; map) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForHash().putAll(key, map);</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: HashSet 并设置时间
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:14
&nbsp;     * @Param: [key, map, time] 键,对应多个键值,时间(秒)
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean true成功 false失败
&nbsp;     **/
&nbsp;    public boolean hmset(String key, Map&lt;String, Object&gt; map, long time) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForHash().putAll(key, map);</b>
<b class="nc">&nbsp;            if (time &gt; 0) {</b>
<b class="nc">&nbsp;                expire(key, time);</b>
&nbsp;            }
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 向一张hash表中放入数据, 如果不存在将创建
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:15
&nbsp;     * @Param: [key, item, value] 键,项,值
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean true 成功 false失败
&nbsp;     **/
&nbsp;    public boolean hset(String key, String item, Object value) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForHash().put(key, item, value);</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 向一张hash表中放入数据, 如果不存在将创建, 并设置时间
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:16
&nbsp;     * @Param: [key, item, value, time] 键,项,值,如果已存在的hash表有时间,这里将会替换原有的时间
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean true 成功 false失败
&nbsp;     **/
&nbsp;    public boolean hset(String key, String item, Object value, long time) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForHash().put(key, item, value);</b>
<b class="nc">&nbsp;            if (time &gt; 0) {</b>
<b class="nc">&nbsp;                expire(key, time);</b>
&nbsp;            }
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 删除hash表中的值
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:18
&nbsp;     * @Param: [key, item] key:不能为null,item:项 可以使多个 不能为null
&nbsp;     * @version: 1.0
&nbsp;     * @Return: void
&nbsp;     **/
&nbsp;    public void hdel(String key, Object... item) {
<b class="nc">&nbsp;        redisTemplate.opsForHash().delete(key, item);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * @Description: 判断hash表中是否有该项的值
&nbsp;     * @Author: royxu
&nbsp;     * @Date: 2025/4/23  14:18
&nbsp;     * @Param: [key, item] 不能为null
&nbsp;     * @version: 1.0
&nbsp;     * @Return: boolean true 存在 false不存在
&nbsp;     **/
&nbsp;    public boolean hHasKey(String key, String item) {
<b class="nc">&nbsp;        return redisTemplate.opsForHash().hasKey(key, item);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * hash递增 如果不存在,就会创建一个 并把新增后的值返回
&nbsp;     *
&nbsp;     * @param key  键
&nbsp;     * @param item 项
&nbsp;     * @param by   要增加几(大于0)
&nbsp;     * @return
&nbsp;     */
&nbsp;    public double hincr(String key, String item, double by) {
<b class="nc">&nbsp;        return redisTemplate.opsForHash().increment(key, item, by);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * hash递减
&nbsp;     *
&nbsp;     * @param key  键
&nbsp;     * @param item 项
&nbsp;     * @param by   要减少记(小于0)
&nbsp;     * @return
&nbsp;     */
&nbsp;    public double hdecr(String key, String item, double by) {
<b class="nc">&nbsp;        return redisTemplate.opsForHash().increment(key, item, -by);</b>
&nbsp;    }
&nbsp;
&nbsp;    // ============================set=============================
&nbsp;
&nbsp;    /**
&nbsp;     * 根据key获取Set中的所有值
&nbsp;     *
&nbsp;     * @param key 键
&nbsp;     * @return
&nbsp;     */
&nbsp;    public Set&lt;Object&gt; sGet(String key) {
&nbsp;        try {
<b class="nc">&nbsp;            return redisTemplate.opsForSet().members(key);</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return null;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 根据value从一个set中查询,是否存在
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param value 值
&nbsp;     * @return true 存在 false不存在
&nbsp;     */
&nbsp;    public boolean sHasKey(String key, Object value) {
&nbsp;        try {
<b class="nc">&nbsp;            return redisTemplate.opsForSet().isMember(key, value);</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 将数据放入set缓存
&nbsp;     *
&nbsp;     * @param key    键
&nbsp;     * @param values 值 可以是多个
&nbsp;     * @return 成功个数
&nbsp;     */
&nbsp;    public long sSet(String key, Object... values) {
&nbsp;        try {
<b class="nc">&nbsp;            return redisTemplate.opsForSet().add(key, values);</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return 0;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 将set数据放入缓存
&nbsp;     *
&nbsp;     * @param key    键
&nbsp;     * @param time   时间(秒)
&nbsp;     * @param values 值 可以是多个
&nbsp;     * @return 成功个数
&nbsp;     */
&nbsp;    public long sSetAndTime(String key, long time, Object... values) {
&nbsp;        try {
<b class="nc">&nbsp;            Long count = redisTemplate.opsForSet().add(key, values);</b>
<b class="nc">&nbsp;            if (time &gt; 0)</b>
<b class="nc">&nbsp;            {expire(key, time);}</b>
<b class="nc">&nbsp;            return count;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return 0;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 获取set缓存的长度
&nbsp;     *
&nbsp;     * @param key 键
&nbsp;     * @return
&nbsp;     */
&nbsp;    public long sGetSetSize(String key) {
&nbsp;        try {
<b class="nc">&nbsp;            return redisTemplate.opsForSet().size(key);</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return 0;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 移除值为value的
&nbsp;     *
&nbsp;     * @param key    键
&nbsp;     * @param values 值 可以是多个
&nbsp;     * @return 移除的个数
&nbsp;     */
&nbsp;    public long setRemove(String key, Object... values) {
&nbsp;        try {
<b class="nc">&nbsp;            Long count = redisTemplate.opsForSet().remove(key, values);</b>
<b class="nc">&nbsp;            return count;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            e.printStackTrace();</b>
<b class="nc">&nbsp;            return 0;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    // ===============================list=================================
&nbsp;
&nbsp;    /**
&nbsp;     * 获取list缓存的内容
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param start 开始
&nbsp;     * @param end   结束 0 到 -1代表所有值
&nbsp;     * @return
&nbsp;     */
&nbsp;    public List&lt;Object&gt; lGet(String key, long start, long end) {
&nbsp;        try {
<b class="nc">&nbsp;            return redisTemplate.opsForList().range(key, start, end);</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return null;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 获取list缓存的长度
&nbsp;     *
&nbsp;     * @param key 键
&nbsp;     * @return
&nbsp;     */
&nbsp;    public long lGetListSize(String key) {
&nbsp;        try {
<b class="nc">&nbsp;            return redisTemplate.opsForList().size(key);</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return 0;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 通过索引 获取list中的值
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param index 索引 index&gt;=0时， 0 表头，1 第二个元素，依次类推；index&lt;0时，-1，表尾，-2倒数第二个元素，依次类推
&nbsp;     * @return
&nbsp;     */
&nbsp;    public Object lGetIndex(String key, long index) {
&nbsp;        try {
<b class="nc">&nbsp;            return redisTemplate.opsForList().index(key, index);</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return null;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 将list放入缓存
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param value 值
&nbsp;     * @return
&nbsp;     */
&nbsp;    public boolean lSet(String key, Object value) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForList().rightPush(key, value);</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 将list放入缓存
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param value 值
&nbsp;     * @param time  时间(秒)
&nbsp;     * @return
&nbsp;     */
&nbsp;    public boolean lSet(String key, Object value, long time) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForList().rightPush(key, value);</b>
<b class="nc">&nbsp;            if (time &gt; 0)</b>
<b class="nc">&nbsp;            {expire(key, time);}</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 将list放入缓存
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param value 值
&nbsp;     * @return
&nbsp;     */
&nbsp;    public boolean lSet(String key, List&lt;Object&gt; value) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForList().rightPushAll(key, value);</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 将list放入缓存
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param value 值
&nbsp;     * @param time  时间(秒)
&nbsp;     * @return
&nbsp;     */
&nbsp;    public boolean lSet(String key, List&lt;Object&gt; value, long time) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForList().rightPushAll(key, value);</b>
<b class="nc">&nbsp;            if (time &gt; 0)</b>
<b class="nc">&nbsp;            {expire(key, time);}</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 根据索引修改list中的某条数据
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param index 索引
&nbsp;     * @param value 值
&nbsp;     * @return
&nbsp;     */
&nbsp;    public boolean lUpdateIndex(String key, long index, Object value) {
&nbsp;        try {
<b class="nc">&nbsp;            redisTemplate.opsForList().set(key, index, value);</b>
<b class="nc">&nbsp;            return true;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return false;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * 移除N个值为value
&nbsp;     *
&nbsp;     * @param key   键
&nbsp;     * @param count 移除多少个
&nbsp;     * @param value 值
&nbsp;     * @return 移除的个数
&nbsp;     */
&nbsp;    public long lRemove(String key, long count, Object value) {
&nbsp;        try {
<b class="nc">&nbsp;            Long remove = redisTemplate.opsForList().remove(key, count, value);</b>
<b class="nc">&nbsp;            return remove;</b>
&nbsp;        } catch (Exception e) {
<b class="nc">&nbsp;            log.error(e.getMessage(), e);</b>
<b class="nc">&nbsp;            return 0;</b>
&nbsp;        }
&nbsp;    }
&nbsp;
&nbsp;    public boolean setObject(String key, Object value, long time) {
<b class="nc">&nbsp;        String json = JSONUtil.toJsonStr(value);</b>
<b class="nc">&nbsp;        return this.set(key, json, time);</b>
&nbsp;    }
&nbsp;
&nbsp;    public &lt;T&gt; T getObject(String key, Class&lt;T&gt; parametrized) {
<b class="nc">&nbsp;        Object obj = this.get(key);</b>
<b class="nc">&nbsp;        if (obj == null) {</b>
<b class="nc">&nbsp;            return null;</b>
&nbsp;        } else {
<b class="nc">&nbsp;            return JSONUtil.toBean(obj.toString(), parametrized);</b>
&nbsp;        }
&nbsp;    }
&nbsp;}
</code>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
      hljs.highlightAll()
      hljs.initLineNumbersOnLoad();
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2025-06-15 18:23</div>
</div>
</body>
</html>
